package com.hhkj.admin.order.service.base.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.hhkj.admin.order.domain.Order;
import com.hhkj.admin.order.mapper.OrderMapper;
import com.hhkj.admin.order.model.dopo.*;
import com.hhkj.admin.order.service.base.OrderRefundService;
import com.hhkj.admin.order.service.base.OrderService;
import com.ruoyi.common.base.biz.service.base.BaseServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author admin
 */
@Service
public class OrderServiceImpl
        extends BaseServiceImpl<Order, OrderDo, OrderPo, OrderMapper>
        implements OrderService {

    @Autowired
    private OrderRefundService orderRefundService;

    @Override
    public OrderPo getPoById(Long id) {
        OrderPo orderPo = super.getPoById(id);
        fillRefundPoList(orderPo);
        return orderPo;
    }

    @Override
    public List<OrderPo> listIncludeItem(OrderDo orderDo) {
        List<Long> idList = getBaseMapper().listOrderIdByListIncludeItem(orderDo);
        List<OrderPo> orderPoList = getBaseMapper().listIncludeItem(idList, orderDo);
        Page<OrderPo> page = BeanUtil.toBean(idList, Page.class);
        page.clear();
        page.addAll(orderPoList);
        return page;
    }

    @Override
    public OrderCountPo countBySaleId(Long saleId) {
        return getBaseMapper().countBySaleId(saleId);
    }

    @Override
    public long countByToday() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = simpleDateFormat.format(new Date());
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<Order>()
                .ge(Order::getCreateTime, date);
        return count(queryWrapper);
    }

    private void fillRefundPoList(OrderPo orderPo) {
        OrderRefundDo orderRefundDo = new OrderRefundDo();
        orderRefundDo.setOrderId(orderPo.getId());
        List<OrderRefundPo> orderRefundPoList = orderRefundService.listPoByDo(orderRefundDo);
        List<OrderRefundPo> collect = orderRefundPoList.stream().filter(orderRefundPo ->
                StringUtils.isNotBlank(orderRefundPo.getAccount()) || StringUtils.isNotBlank(orderRefundPo.getAccountName()))
                .collect(Collectors.toList());
        orderPo.setOrderRefundPoList(collect);
    }
}




